﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Linq.Expressions;
using Newtonsoft.Json;
using OA.Core;
using OA.Model;
using OA.BLL;
using Microsoft.Extensions.Caching.Memory;
using OA.Web.Code;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace OA.Web.Areas.Admin.Controllers
{
    public class FormGroupController : BaseAdminController
    {
        private IMemoryCache _cache;
        public FormGroupController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
        }

        public IActionResult Index()
        {            
            return View();
        }

        public IActionResult GetData(string keyword, int page = 1, int limit = 20)
        {
            Expression<Func<FormGroup, bool>> where = m => true;
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                where = m => (m.Name.Contains(keyword));
            }
            PageList<SimpleFormGroup> list = this.GetPageList<FormGroup, SimpleFormGroup>(where, page, limit, SimpleFormGroup.Map);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.RowCount,
                data = list.List
            };
            return Json(result);
        }

        [HttpGet]
        public IActionResult Create()
        {            
            return View(new FormGroup());
        }




        [HttpPost]
        public IActionResult Create(FormGroup model)
        {
            BaseResult baseResult = new BaseResult();
            if (!ModelState.IsValid)
            {
                baseResult.Msg = this.GetModelStateError(ModelState);
                baseResult.State = 400;
                return Json(baseResult);
            }
            else
            {
                this.AddInfo<FormGroup>(model);
                baseResult.Msg = "表单分组添加成功！";
                return Json(baseResult);

            }

        }

        [HttpGet]
        public IActionResult Edit(int id)
        {
            FormGroup infoFormGroup = this.GetInfo<FormGroup>(id);
            if (infoFormGroup == null)
            {
                return Redirect("/Admin/Home/Info?msg=" + System.Web.HttpUtility.UrlEncode("沒有找到该表单分组", Encoding.UTF8));
            }
            return View(infoFormGroup);
        }

        [HttpPost]
        public IActionResult Edit(int id, FormGroup model)
        {
            BaseResult baseResult = new BaseResult();
            if (!ModelState.IsValid)
            {
                baseResult.Msg = this.GetModelStateError(ModelState);
                baseResult.State = 400;
                return Json(baseResult);
            }
            FormGroup infoPageGroup = this.GetInfo<FormGroup>(id);
            if (infoPageGroup == null)
            {
                baseResult.Msg = "沒有找到该表单分组";
                baseResult.State = 400;
                return Json(baseResult);
            }
            this.Update<FormGroup>(infoPageGroup, model, new string[] { "Name" });

            baseResult.Msg = "表单分组修改成功！";
            return Json(baseResult);

        }

        [HttpGet]
        public IActionResult Delete(int id)
        {
            Expression<Func<FormGroup, bool>> where = m => m.ID == id;
            this.Delete<FormGroup>(where);
            return Json(new { State = 200, Msg = "" });
        }

    }
}